<?php
declare(strict_types=1);

use Phinx\Db\Adapter\MysqlAdapter;
use Phinx\Migration\AbstractMigration;

final class CreateTagRelationTable extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function up()
    {
        $table = $this->table('tag_relation', ['id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '标签关联表']);
        $table->addColumn('id', 'integer', ['identity' => true, 'signed' => false, 'limit' => 11, 'comment' => '主键ID'])
            ->addColumn('relation_type', 'integer', ['limit' => 255, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '关联类型。1:需求|2:工单|3:延期|4:延期环节确认|5:延期制片拒绝|6:任务返修'])
            ->addColumn('relation_id', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '关联id'])
            ->addColumn('tag_id', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '标签id'])
            ->addColumn('step_category_id', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '工序分类ID'])
            ->addColumn('entity_ids', 'text', ['null' => true, 'comment' => '实体id集合(以,分隔)'])
            ->addColumn('created', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '创建时间'])
            ->addColumn('created_by', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '创建者'])
            ->addIndex(['relation_type'], ['type' => 'normal', 'name' => 'relation_type'])
            ->addIndex(['relation_id'], ['type' => 'normal', 'name' => 'relation_id']);
        // 创建表
        $table->create();
    }

    public function down()
    {
        $this->table('tag_relation')->drop()->save();
    }
}
